const fs = require('fs')
const path = require('path');
exports.upload=async function(files){
    var loca=path.resolve('').replace(' ','')+'/';
    var d=new Date();
    var _newpath='upload/upfile/'+d.getFullYear()+'_'+(d.getMonth()+1)+"_"+d.getDate()
    //console.log(loca+newpath);
    var nf=[];
    mkdir(_newpath,loca);
    for(var i=0;i<files.length;i++){
        var f=files[i];
        var tmpath=f.path;
        var type=f.name.match(/\.[^\.]+$/g)[0];
        var newpath=_newpath+"/"+Date.parse(new Date()).toString()+Math.random().toString(32).substr(2)+type;
        nf.push(newpath.substr(6));
        var st=fs.createWriteStream(loca+newpath);
        await fs.createReadStream(tmpath).pipe(st)
    }
    return nf;
}
//创建多层目录
function mkdir(path,l){
	if(!fs.existsSync(l+path)){
		var d=l;
		path.split('/').forEach(function(p){
            d+=p+'/'
            if(!fs.existsSync(d)){
                fs.mkdirSync(d)
            }
		})
	}
	return true;
}
exports.mkdir=mkdir;
function format(time,fmt) { 

    var o = { 
       "M+" : time.getMonth()+1,                 //月份 
       "d+" : time.getDate(),                    //日 
       "h+" : time.getHours(),                   //小时 
       "m+" : time.getMinutes(),                 //分 
       "s+" : time.getSeconds(),                 //秒 
       "q+" : Math.floor((time.getMonth()+3)/3), //季度 
       "S"  : time.getMilliseconds()             //毫秒 
   }; 
   if(/(y+)/.test(fmt)) {
           fmt=fmt.replace(RegExp.$1, (time.getFullYear()+"").substr(4 - RegExp.$1.length)); 
   }
   for(var k in o) {
       if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
   }
   return fmt; 
} 
exports.format=format;